package org.linlinjava.litemall.core.lock_rfid;

import cn.hutool.core.util.ObjectUtil;
import org.linlinjava.litemall.core.util.InventoryUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.AgedmallCabinetGoodsMapper;
import org.linlinjava.litemall.db.dao.AgedmallCabinetMapper;
import org.linlinjava.litemall.db.dao.AgedmallGoodsEquMapper;
import org.linlinjava.litemall.db.dao.AgedmallRfidGoodsProductMapper;
import org.linlinjava.litemall.db.dao.app.AppOrderMapper;
import org.linlinjava.litemall.db.dao.order_manage.AgedmallLocksMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.rfid.AgedmallLgRfidStateService;
import org.linlinjava.litemall.db.vo.locks.CanUseCabinetGoodsDto;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wj
 * @date 2021/11/15 11:17
 */
@Component
public class LockRfidUtil {

    @Resource
    private AgedmallGoodsEquMapper agedmallGoodsEquMapper;

    @Resource
    private InventoryUtil inventoryUtil;

    @Resource
    private AgedmallLgRfidStateService agedmallLgRfidStateService;

    @Resource
    private AgedmallRfidGoodsProductMapper agedmallRfidGoodsProductMapper;

    @Resource
    private AgedmallCabinetMapper agedmallCabinetMapper;

    @Resource
    private AgedmallCabinetGoodsMapper agedmallCabinetGoodsMapper;

    @Resource
    private AgedmallLocksMapper agedmallLocksMapper;

    @Resource
    private AppOrderMapper appOrderMapper;


    public List<String> lockGoods(LockGoodsVo lockGoodsVo){
        if(lockGoodsVo.getNumber()==null){
            lockGoodsVo.setNumber(1);
        }
        //医院库存
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        if(lockGoodsVo.getType()==2){
            List<Long> canUseHospitalEquIdList = appOrderMapper.getCanUseHospitalEquIdList(lockGoodsVo.getHospitalSelfLocationId().getId());
            goodsEquExample.or().andDeletedEqualTo((byte)0).andStoreIdEqualTo(lockGoodsVo.getStoreId()).
                    andGoodsIdEqualTo(lockGoodsVo.getGoodsId().longValue()).andGoodsProductIdEqualTo(lockGoodsVo.getSpecId().longValue())
                    .andTypeEqualTo(lockGoodsVo.getType()).andStateEqualTo(0).andEquIdIn(canUseHospitalEquIdList);
        }
        else{
            goodsEquExample.or().andDeletedEqualTo((byte)0).andStoreIdEqualTo(lockGoodsVo.getStoreId()).
                    andGoodsIdEqualTo(lockGoodsVo.getGoodsId().longValue()).andGoodsProductIdEqualTo(lockGoodsVo.getSpecId().longValue())
                    .andTypeEqualTo(lockGoodsVo.getType()).andStateEqualTo(0);
        }

        List<AgedmallGoodsEqu> goodsEquList = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        if(ObjectUtil.isNull(goodsEquList)){
            return null;
        }
        if(goodsEquList.size()<lockGoodsVo.getNumber()){
           return null;
        }
        List<String> backEquIds=new ArrayList<>();
        for (int i = 0; i < lockGoodsVo.getNumber(); i++) {
            AgedmallGoodsEqu goodsEqu=goodsEquList.get(i);
            //锁定商品
            goodsEqu.setState(4);
            goodsEqu.setDistributionType(1);
            agedmallGoodsEquMapper.updateByPrimaryKey(goodsEqu);
            backEquIds.add(goodsEqu.getEquId().toString());
            //修改agedmall_lg_rfid_goods_cur_stat状态
            if(lockGoodsVo.getType()!=2){
                //锁定库存
                inventoryUtil.remove(lockGoodsVo.getStoreId().intValue(),0,1,lockGoodsVo.getGoodsId(),lockGoodsVo.getSpecId());
                agedmallLgRfidStateService.changeLgRfidState(goodsEqu.getEquId().toString(),1,1);
            }
        }

        return backEquIds;
    }



    public Object unlockGoods(UnLockGoodsVo unLockGoodsVo){
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        List<Long> equRfids=new ArrayList<>();
        List<String> rfid = unLockGoodsVo.getRfid();
        rfid.forEach( r ->equRfids.add(Long.parseLong(r)));
        goodsEquExample.or().andDeletedEqualTo((byte)0).andEquIdIn(equRfids).andTypeEqualTo(unLockGoodsVo.getType());
        List<AgedmallGoodsEqu> goodsEquList = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        for (AgedmallGoodsEqu goodsEqu:goodsEquList) {
            goodsEqu.setState(0);
            goodsEqu.setDistributionType(0);
            //解锁商品
            agedmallGoodsEquMapper.updateByPrimaryKey(goodsEqu);
            if(unLockGoodsVo.getType()!=2){
                //库存增加
                inventoryUtil.add(goodsEqu.getStoreId().intValue(),0,1,goodsEqu.getGoodsId().intValue()
                        ,goodsEqu.getGoodsProductId().intValue());
                //修改agedmall_lg_rfid_goods_cur_stat状态
                agedmallLgRfidStateService.changeLgRfidState(goodsEqu.getEquId().toString(),0,1);
            }

        }
        return ResponseUtil.ok();
    }


    public List<String> lockCabinetGoods(LockGoodsVo lockGoodsVo){
        if(lockGoodsVo.getNumber()==null){
            lockGoodsVo.setNumber(1);
        }
        AgedmallCabinetExample cabinetExample=new AgedmallCabinetExample();
        cabinetExample.or().andDeletedEqualTo((byte)0).andStoreIdEqualTo(lockGoodsVo.getStoreId());
        AgedmallCabinet cabinet = agedmallCabinetMapper.selectOneByExample(cabinetExample);
        if(ObjectUtil.isNull(cabinet)){
         return null;
        }
        List<CanUseCabinetGoodsDto> canUseCabinetGoodsDtos = agedmallLocksMapper.selectCanUseCabinetGoodsId(cabinet.getId()
                , lockGoodsVo.getStoreId(), lockGoodsVo.getGoodsId().longValue(),lockGoodsVo.getSpecId().longValue());
        if(ObjectUtil.isNull(canUseCabinetGoodsDtos)){
            return null;
        }
        if(canUseCabinetGoodsDtos.size()<lockGoodsVo.getNumber()){
            return null;
        }
        List<String> rfidList=new ArrayList<>();
        for (int i = 0; i < lockGoodsVo.getNumber(); i++) {
            CanUseCabinetGoodsDto canUseCabinetGoodsDto=canUseCabinetGoodsDtos.get(i);
            //锁定库存
            inventoryUtil.remove(lockGoodsVo.getStoreId().intValue(),0,2,lockGoodsVo.getGoodsId(),lockGoodsVo.getSpecId());
            //锁定商品
            AgedmallCabinetGoodsExample goodsExample=new AgedmallCabinetGoodsExample();
            goodsExample.or().andIdEqualTo(canUseCabinetGoodsDto.getCanbinetGoodsId());
            AgedmallCabinetGoods cabinetGoods=new AgedmallCabinetGoods();
            cabinetGoods.setStat(3);
            agedmallCabinetGoodsMapper.updateByExampleSelective(cabinetGoods,goodsExample);

            AgedmallRfidGoodsProductExample goodsProductExample=new AgedmallRfidGoodsProductExample();
            goodsProductExample.or().andIdEqualTo(canUseCabinetGoodsDto.getRfidGoodsProductId());
            AgedmallRfidGoodsProduct goodsProduct=new AgedmallRfidGoodsProduct();
            goodsProduct.setStat(5);
            agedmallRfidGoodsProductMapper.updateByExampleSelective(goodsProduct,goodsProductExample);
            rfidList.add(canUseCabinetGoodsDto.getRfid());
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(canUseCabinetGoodsDto.getRfid(),1,2);
        }


        return rfidList;
    }


    public void unlockCabinetGoods(UnLockCabinetGoodsVo unLockGoodsVo){
        AgedmallRfidGoodsProductExample goodsProductExample=new AgedmallRfidGoodsProductExample();
        goodsProductExample.or().andRfidIn(unLockGoodsVo.getRfid()).andDeletedEqualTo(false);
        List<AgedmallRfidGoodsProduct> agedmallRfidGoodsProductList = agedmallRfidGoodsProductMapper.selectByExample(goodsProductExample);

        for (AgedmallRfidGoodsProduct agedmallRfidGoodsProduct:agedmallRfidGoodsProductList) {
            //库存增加
            inventoryUtil.add(agedmallRfidGoodsProduct.getStoreId().intValue(),0,2,agedmallRfidGoodsProduct.getGoodsId().intValue()
                    ,agedmallRfidGoodsProduct.getGoodsProductId().intValue());
            //解锁商品
            AgedmallCabinetGoodsExample goodsExample=new AgedmallCabinetGoodsExample();
            goodsExample.or().andDeletedEqualTo(false).andGoodsRfidIdEqualTo(agedmallRfidGoodsProduct.getId());
            AgedmallCabinetGoods cabinetGoods=new AgedmallCabinetGoods();
            cabinetGoods.setStat(0);
            agedmallCabinetGoodsMapper.updateByExampleSelective(cabinetGoods,goodsExample);
            agedmallRfidGoodsProduct.setStat(0);
            agedmallRfidGoodsProductMapper.updateByExampleSelective(agedmallRfidGoodsProduct,goodsProductExample);
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(agedmallRfidGoodsProduct.getRfid(),0,2);
        }

    }


    /**
     * 配货医柜使用，锁定新的指定rfid
     * @return
     */
    public void replaceLockCabinetGoods(UnLockCabinetGoodsVo unLockGoodsVo){


        AgedmallRfidGoodsProductExample goodsProductExample=new AgedmallRfidGoodsProductExample();
        goodsProductExample.or().andRfidIn(unLockGoodsVo.getRfid()).andDeletedEqualTo(false);
        List<AgedmallRfidGoodsProduct> agedmallRfidGoodsProductList = agedmallRfidGoodsProductMapper.selectByExample(goodsProductExample);

        for (AgedmallRfidGoodsProduct agedmallRfidGoodsProduct:agedmallRfidGoodsProductList) {
            //锁定库存
            inventoryUtil.remove(agedmallRfidGoodsProduct.getStoreId().intValue(),0,2
                    ,agedmallRfidGoodsProduct.getGoodsId().intValue(),
                    agedmallRfidGoodsProduct.getGoodsProductId().intValue());
            //锁定商品
            AgedmallCabinetGoodsExample goodsExample=new AgedmallCabinetGoodsExample();
            goodsExample.or().andDeletedEqualTo(false).andGoodsRfidIdEqualTo(agedmallRfidGoodsProduct.getId());
            AgedmallCabinetGoods cabinetGoods=new AgedmallCabinetGoods();
            cabinetGoods.setStat(3);
            agedmallCabinetGoodsMapper.updateByExampleSelective(cabinetGoods,goodsExample);
            AgedmallRfidGoodsProduct goodsProduct=new AgedmallRfidGoodsProduct();
            goodsProduct.setStat(5);
            agedmallRfidGoodsProductMapper.updateByExampleSelective(goodsProduct,goodsProductExample);
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(agedmallRfidGoodsProduct.getRfid(),1,2);
        }

    }
}
